﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CNNetworking;
using BobcatUtility;

namespace CNTestApp
{
    /// <summary>
    /// This control shows a list of ConnectionSettings items. It wraps
    /// a ListView control, and lets you show and retrieve ConnectionSettings
    /// objects easily.
    /// </summary>
    public partial class Control_ConnectionSettingsList : UserControl
    {
        #region Events

        /// <summary>
        /// Data passed with events raised by this class.
        /// </summary>
        public class Args : EventArgs
        {
            public ConnectionSettings SelectedItem { get; set; }
        }

        /// <summary>
        /// Event raised when an item in the list is double-clicked.
        /// </summary>
        public event EventHandler<Args> ItemDoubleClickedEvent;

        #endregion

        #region Public properties

        /// <summary>
        /// Sets the collection of ConnectionSettings objects to be shown in the list.
        /// </summary>
        public IList<ConnectionSettings> ConnectionSettings
        {
            set
            {
                // We add the items to the list...
                ctrlListView.Items.Clear();
                foreach (ConnectionSettings cs in value)
                {
                    ListViewItem item = new ListViewItem(cs.FirstName);
                    item.SubItems.Add(cs.LastName);
                    item.SubItems.Add(cs.HostName);
                    item.Tag = cs;
                    ctrlListView.Items.Add(item);
                }

                // We size the columns to fit the contents...
                ctrlListView.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);

                // We select the first item...
                if (ctrlListView.Items.Count > 0)
                {
                    ctrlListView.Items[0].Selected = true;
                }
            }
        }

        /// <summary>
        /// Gets the currently selected item. Returns null if there
        /// is no currently selected item (or more than one).
        /// </summary>
        public ConnectionSettings SelectedItem
        {
            get
            {
                if (ctrlListView.SelectedItems.Count != 1)
                {
                    return null;
                }
                ConnectionSettings cs = (ConnectionSettings)ctrlListView.SelectedItems[0].Tag;
                return cs;
            }
        }

        #endregion

        #region Public methods

        /// <summary>
        /// Constructor.
        /// </summary>
        public Control_ConnectionSettingsList()
        {
            InitializeComponent();
        }

        #endregion

        #region Private functions

        /// <summary>
        /// Called when the control is loaded. 
        /// </summary>
        private void Control_ConnectionSettingsList_Load(object sender, EventArgs e)
        {
            // We set up the columns...
            ctrlListView.Columns.Add("First name");
            ctrlListView.Columns.Add("Last name");
            ctrlListView.Columns.Add("Computer name");
        }

        /// <summary>
        /// Called when an item in the list is double-clicked.
        /// </summary>
        private void ctrlListView_DoubleClick(object sender, EventArgs e)
        {
            // We raise our own double-click event, passing the ConnectionSettings
            // object that was clicked...
            if (ctrlListView.SelectedItems.Count != 1)
            {
                return;
            }
            ConnectionSettings cs = (ConnectionSettings)ctrlListView.SelectedItems[0].Tag;
            Utility.raiseEvent(ItemDoubleClickedEvent, this, new Args { SelectedItem = cs });
        }

        #endregion

    }
}
